package top.kaoshanji.p1ch02b;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.lang.reflect.Proxy;

/**
 * 代码清单 p1ch02b-02：创建和使用动态代理的示例(2.3.1节)
 * author kaoshanji
 * ^_^
 * create time 2024/12/23 18:04
 */
public class SimpleProxyV1 {

    private static final Logger logger = LogManager.getLogger(SimpleProxyV1.class);

    public static void main(String[] args) {
        logger.info("...代码清单 p1ch02b-02：创建和使用动态代理的示例(2.3.1节)...");

        String str = "Hello World";
        LoggingInvocationHandler handler = new LoggingInvocationHandler(str);
        ClassLoader cl = SimpleProxyV1.class.getClassLoader();

        // 传递什么接口，就可以调用该接口的方法
        // 但是，具体的逻辑是在 LoggingInvocationHandler 里面处理
        Comparable obj = (Comparable)Proxy.newProxyInstance(cl, new Class[]{Comparable.class}, handler);

        obj.compareTo("Good");

    }

}
